package handler

import (
	"github.com/zeromicro/go-zero/core/logx"
	"net/http"

	"cloud-disk/core/internal/logic"
	"cloud-disk/core/internal/svc"
	"cloud-disk/core/internal/types"
	"github.com/zeromicro/go-zero/rest/httpx"
)

func UserLoginHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		logx.Info("开始处理登录请求")
		var req types.LoginRequest
		if err := httpx.Parse(r, &req); err != nil {
			logx.Errorf("解析请求失败: %v", err)
			httpx.ErrorCtx(r.Context(), w, err)
			return
		}
		logx.Infof("解析请求成功: %+v", req)

		l := logic.NewUserLoginLogic(r.Context(), svcCtx)
		resp, err := l.UserLogin(&req)
		if err != nil {
			logx.Errorf("登录失败: %v", err)
			httpx.ErrorCtx(r.Context(), w, err)
		} else {
			logx.Info("登录成功")
			httpx.OkJsonCtx(r.Context(), w, resp)
		}
	}
}
